package me.leiusual.sharing.ui

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Address
import me.leiusual.sharing.api.model.Bank
import me.leiusual.sharing.api.model.ProductDetail
import me.leiusual.sharing.api.model.StoreCategories
import me.leiusual.sharing.util.extension.requireValue
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class MainViewModel @Inject constructor(

) : ViewModel() {

  // 分类id：用于商区分类中的类别选择

  private val _category = MutableLiveData<SelectedCategory>()
  val category: LiveData<SelectedCategory> = _category
  fun category(category: StoreCategories.Response.Category.Item, level: Int) {
    when (level) {
      0 -> {
        _category.value = SelectedCategory(category, null, null)
      }
      1 -> {
        _category.value = _category.requireValue.copy(level1 = category, level2 = null)
      }
      2 -> {
        _category.value = _category.requireValue.copy(level2 = category)
      }
    }
  }

  data class SelectedCategory(
      val level0: StoreCategories.Response.Category.Item,
      val level1: StoreCategories.Response.Category.Item?,
      val level2: StoreCategories.Response.Category.Item?
  )

  // 商品详情：用于添加购物车时展示可选属性等

  private val _productDetail = MutableLiveData<ProductDetail>()
  val productDetail: LiveData<ProductDetail> = _productDetail
  fun productDetail(productDetail: ProductDetail) {
    _productDetail.value = productDetail
  }

  // 搜索关键词：用于首页的商品搜索、店铺搜索

  private val _searchKeyword = MutableLiveData<String>()
  val searchKeyword: LiveData<String> = _searchKeyword
  fun searchKeyword(keyword: String?) {
    _searchKeyword.value = keyword
  }

  // 选中的地址信息：用于购物车生成订单时使用

  private val _address = MutableLiveData<Address>()
  val address: LiveData<Address> = _address
  fun address(address: Address?) {
    _address.value = address
  }

  // 选中的银行账户：用于创建申请提现时使用

  private val _bank = MutableLiveData<Bank>()
  val bank: LiveData<Bank> = _bank
  fun bank(bank: Bank?) {
    _bank.value = bank
  }

  // 当前订单数量统计：用于列表页面同步给TAB页面

  private val _orderCount = MutableLiveData<Map<String, Long>>()
  val orderCount: LiveData<Map<String, Long>> = _orderCount
  fun orderCount(orderCount: Map<String, Long>) {
    _orderCount.value = orderCount
  }
}